package com.sann.demo.service.impl;

import com.sann.demo.dao.IAccountDao;
import com.sann.demo.domain.Account;
import com.sann.demo.service.IAccountService;

public class AccountServiceImpl implements IAccountService {

    private IAccountDao accountDao;

    public void setAccountDao(IAccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public Account findAccountById(Integer accountId) {
        Account account = this.accountDao.findAccountById(accountId);
        return account;
    }

    @Override
    public void transfer(String sourceName, String targetName, float money) {
        //2.1.查询转出账户
        Account source = accountDao.findAccountByName(sourceName);
        //2.2.查询转入账户
        Account target = accountDao.findAccountByName(targetName);
        //2.3.转出账户减钱
        source.setMoney(source.getMoney() - money);
        //2.4.转入账户加钱
        target.setMoney(target.getMoney() + money);
        //2.5.更新转出账户
        accountDao.updateAccount(source);

        // 引入出现业务异常逻辑
        int i = 1/0;

        //2.6.更新转入账户
        accountDao.updateAccount(target);
    }
}
